package com.px.lovepath.ui.general;

import java.util.Date;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;

import com.px.lovepath.R;
import com.px.lovepath.ui.general.wheel.NumericWheelAdapter;
import com.px.lovepath.ui.general.wheel.OnWheelChangedListener;
import com.px.lovepath.ui.general.wheel.WheelView;

public class PickDateDialog extends Dialog implements OnWheelChangedListener {
	
	private NumericWheelAdapter mAdapter;
	private final WheelView mYear;
    private final WheelView mMonth;
    private final WheelView mDay;
    private final Button mButton;
    private final Date mDate;
    private final OnChangedLisenter mListener;
    
	public PickDateDialog(Context context, Date date, OnChangedLisenter lisenter) {
		super(context, R.style.full_height_dialog);
		setContentView(R.layout.general__pick_date_view);
		mDate = date;
		mListener = lisenter;
		
		mYear = (WheelView) findViewById(R.id.general__pick_date_view__year);
		mYear.setAdapter(new NumericWheelAdapter(1970, new Date().getYear() + 1900));
		mYear.setLabel("年");
		mYear.addChangingListener(this);
		mYear.setCurrentItem(mDate.getYear() - 70);
		
		mMonth = (WheelView) findViewById(R.id.general__pick_date_view__month);
		mMonth.setAdapter(new NumericWheelAdapter(1, 12, "%02d"));
		mMonth.setLabel("月");
		mMonth.setCyclic(true);
		mMonth.addChangingListener(this);
		mMonth.setCurrentItem(mDate.getMonth());          
		
		mDay = (WheelView) findViewById(R.id.general__pick_date_view__day);
		mAdapter = new NumericWheelAdapter(1, 31, "%02d");
		mDay.setAdapter(mAdapter);
		mDay.setLabel("日");
		mDay.setCyclic(true);
		mDay.setCurrentItem(mDate.getDate() - 1);
		
		mButton = (Button) findViewById(R.id.general__pick_date_view__done);
		mButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Date temp = getSelectDate();
				if (mDate !=  temp && mListener != null) {
					mListener.onChanged(temp);
				}
				dismiss();
			}
		});
	}
	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if (mAdapter == null) {
			return;
		}
		int max = mAdapter.getMaxValue();
		int newMax = 0;
		if (mYear == wheel) {
			newMax = getDay(newValue + 1900, mMonth.getCurrentItem() + 1);
		} else {
			newMax = getDay(mYear.getCurrentItem() + 1900, newValue + 1);
		}
		
		if (max != newMax) {
			mAdapter.setMaxValue(newMax);
			mDay.setCurrentItem(0);
		}
	}
	
	// ### 实现 ####
	private int getDay(int year, int month) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
			return 31;
		} else if (month != 2) {
			return 30;
		} else {
			if(year % 400 == 0|| (year % 4 == 0 && year % 100 != 0) ) {
				return 29;
			} else {
				return 28;
			}
		}
	}
	private Date getSelectDate() {
		Date date = new Date();
		date.setYear(mYear.getCurrentItem() + 70);
		date.setMonth(mMonth.getCurrentItem());
		date.setDate(mDay.getCurrentItem() + 1);
		return date;
	}
	
	// ### 内部类  ###
	public interface OnChangedLisenter{
		void onChanged(Date date);
	}
}
